package com.genymobile.scrcpy;

import android.app.Application;
import android.content.AttributionSource;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.ApplicationInfo;
import android.media.AudioAttributes;
import android.media.AudioRecord;
import android.os.Build;
import android.os.Looper;
import android.os.Parcel;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/* loaded from: classes2.dex */
public final class Workarounds {
    private static final Object ACTIVITY_THREAD;
    private static final Class<?> ACTIVITY_THREAD_CLASS;

    static {
        prepareMainLooper();
        try {
            Class<?> cls = Class.forName("android.app.ActivityThread");
            ACTIVITY_THREAD_CLASS = cls;
            Constructor<?> activityThreadConstructor = cls.getDeclaredConstructor(new Class[0]);
            activityThreadConstructor.setAccessible(true);
            Object newInstance = activityThreadConstructor.newInstance(new Object[0]);
            ACTIVITY_THREAD = newInstance;
            Field sCurrentActivityThreadField = cls.getDeclaredField("sCurrentActivityThread");
            sCurrentActivityThreadField.setAccessible(true);
            sCurrentActivityThreadField.set(null, newInstance);
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }

    private Workarounds() {
    }

    public static void apply(boolean audio, boolean camera) {
        boolean mustFillConfigurationController = false;
        boolean mustFillAppInfo = false;
        boolean mustFillAppContext = false;
        if (Build.BRAND.equalsIgnoreCase("meizu")) {
            mustFillAppInfo = true;
        } else if (Build.BRAND.equalsIgnoreCase("honor")) {
            mustFillAppInfo = true;
            mustFillAppContext = true;
        }
        if (audio && Build.VERSION.SDK_INT == 30) {
            mustFillAppContext = true;
        }
        if (camera) {
            mustFillAppInfo = true;
            mustFillAppContext = true;
        }
        if (Build.VERSION.SDK_INT >= 31) {
            mustFillConfigurationController = true;
        }
        if (mustFillConfigurationController) {
            fillConfigurationController();
        }
        if (mustFillAppInfo) {
            fillAppInfo();
        }
        if (mustFillAppContext) {
            fillAppContext();
        }
    }

    private static void prepareMainLooper() {
        Looper.prepareMainLooper();
    }

    private static void fillAppInfo() {
        try {
            Class<?> appBindDataClass = Class.forName("android.app.ActivityThread$AppBindData");
            Constructor<?> appBindDataConstructor = appBindDataClass.getDeclaredConstructor(new Class[0]);
            appBindDataConstructor.setAccessible(true);
            Object appBindData = appBindDataConstructor.newInstance(new Object[0]);
            ApplicationInfo applicationInfo = new ApplicationInfo();
            applicationInfo.packageName = "com.android.shell";
            Field appInfoField = appBindDataClass.getDeclaredField("appInfo");
            appInfoField.setAccessible(true);
            appInfoField.set(appBindData, applicationInfo);
            Field mBoundApplicationField = ACTIVITY_THREAD_CLASS.getDeclaredField("mBoundApplication");
            mBoundApplicationField.setAccessible(true);
            mBoundApplicationField.set(ACTIVITY_THREAD, appBindData);
        } catch (Throwable throwable) {
            Ln.d("Could not fill app info: " + throwable.getMessage());
        }
    }

    private static void fillAppContext() {
        try {
            Application app = new Application();
            Field baseField = ContextWrapper.class.getDeclaredField("mBase");
            baseField.setAccessible(true);
            baseField.set(app, FakeContext.get());
            Field mInitialApplicationField = ACTIVITY_THREAD_CLASS.getDeclaredField("mInitialApplication");
            mInitialApplicationField.setAccessible(true);
            mInitialApplicationField.set(ACTIVITY_THREAD, app);
        } catch (Throwable throwable) {
            Ln.d("Could not fill app context: " + throwable.getMessage());
        }
    }

    private static void fillConfigurationController() {
        try {
            Class<?> configurationControllerClass = Class.forName("android.app.ConfigurationController");
            Class<?> activityThreadInternalClass = Class.forName("android.app.ActivityThreadInternal");
            Constructor<?> configurationControllerConstructor = configurationControllerClass.getDeclaredConstructor(activityThreadInternalClass);
            configurationControllerConstructor.setAccessible(true);
            Object obj = ACTIVITY_THREAD;
            Object configurationController = configurationControllerConstructor.newInstance(obj);
            Field configurationControllerField = ACTIVITY_THREAD_CLASS.getDeclaredField("mConfigurationController");
            configurationControllerField.setAccessible(true);
            configurationControllerField.set(obj, configurationController);
        } catch (Throwable throwable) {
            Ln.d("Could not fill configuration: " + throwable.getMessage());
        }
    }

    static Context getSystemContext() {
        try {
            Method getSystemContextMethod = ACTIVITY_THREAD_CLASS.getDeclaredMethod("getSystemContext", new Class[0]);
            return (Context) getSystemContextMethod.invoke(ACTIVITY_THREAD, new Object[0]);
        } catch (Throwable throwable) {
            Ln.d("Could not get system context: " + throwable.getMessage());
            return null;
        }
    }

//    public static AudioRecord createAudioRecord(int source, int sampleRate, int channelConfig, int channels, int channelMask, int encoding) {
//        Throwable th;
//        int initResult;
//        try {
//            Constructor<AudioRecord> audioRecordConstructor = AudioRecord.class.getDeclaredConstructor(Long.TYPE);
//            audioRecordConstructor.setAccessible(true);
//            AudioRecord audioRecord = audioRecordConstructor.newInstance(0L);
//            Field mRecordingStateField = AudioRecord.class.getDeclaredField("mRecordingState");
//            mRecordingStateField.setAccessible(true);
//            mRecordingStateField.set(audioRecord, 1);
//            Looper looper = Looper.myLooper();
//            if (looper == null) {
//                looper = Looper.getMainLooper();
//            }
//            Field mInitializationLooperField = AudioRecord.class.getDeclaredField("mInitializationLooper");
//            mInitializationLooperField.setAccessible(true);
//            mInitializationLooperField.set(audioRecord, looper);
//            AudioAttributes.Builder audioAttributesBuilder = new AudioAttributes.Builder();
//            Method setInternalCapturePresetMethod = AudioAttributes.Builder.class.getMethod("setInternalCapturePreset", Integer.TYPE);
//            setInternalCapturePresetMethod.invoke(audioAttributesBuilder, Integer.valueOf(source));
//            AudioAttributes attributes = audioAttributesBuilder.build();
//            Field mAudioAttributesField = AudioRecord.class.getDeclaredField("mAudioAttributes");
//            mAudioAttributesField.setAccessible(true);
//            mAudioAttributesField.set(audioRecord, attributes);
//            Method audioParamCheckMethod = AudioRecord.class.getDeclaredMethod("audioParamCheck", Integer.TYPE, Integer.TYPE, Integer.TYPE);
//            audioParamCheckMethod.setAccessible(true);
//            audioParamCheckMethod.invoke(audioRecord, Integer.valueOf(source), Integer.valueOf(sampleRate), Integer.valueOf(encoding));
//            Field mChannelCountField = AudioRecord.class.getDeclaredField("mChannelCount");
//            mChannelCountField.setAccessible(true);
//            mChannelCountField.set(audioRecord, Integer.valueOf(channels));
//            Field mChannelMaskField = AudioRecord.class.getDeclaredField("mChannelMask");
//            mChannelMaskField.setAccessible(true);
//            mChannelMaskField.set(audioRecord, Integer.valueOf(channelMask));
//            try {
//                int minBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, encoding);
//                int bufferSizeInBytes = minBufferSize * 8;
//                Method audioBuffSizeCheckMethod = AudioRecord.class.getDeclaredMethod("audioBuffSizeCheck", Integer.TYPE);
//                audioBuffSizeCheckMethod.setAccessible(true);
//                audioBuffSizeCheckMethod.invoke(audioRecord, Integer.valueOf(bufferSizeInBytes));
//                int[] sampleRateArray = {sampleRate};
//                int[] session = {0};
//                if (Build.VERSION.SDK_INT < 31) {
//                    Method nativeSetupMethod = AudioRecord.class.getDeclaredMethod("native_setup", Object.class, Object.class, int[].class, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, int[].class, String.class, Long.TYPE);
//                    nativeSetupMethod.setAccessible(true);
//                    initResult = ((Integer) nativeSetupMethod.invoke(audioRecord, new WeakReference(audioRecord), attributes, sampleRateArray, Integer.valueOf(channelMask), 0, Integer.valueOf(audioRecord.getAudioFormat()), Integer.valueOf(bufferSizeInBytes), session, FakeContext.get().getOpPackageName(), 0L)).intValue();
//                } else {
//                    AttributionSource attributionSource = FakeContext.get().getAttributionSource();
//                    Method asScopedParcelStateMethod = AttributionSource.class.getDeclaredMethod("asScopedParcelState", new Class[0]);
//                    asScopedParcelStateMethod.setAccessible(true);
//                    AutoCloseable attributionSourceState = (AutoCloseable) asScopedParcelStateMethod.invoke(attributionSource, new Object[0]);
//                    try {
//                        try {
//                            Method getParcelMethod = attributionSourceState.getClass().getDeclaredMethod("getParcel", new Class[0]);
//                            Parcel attributionSourceParcel = (Parcel) getParcelMethod.invoke(attributionSourceState, new Object[0]);
//                            if (Build.VERSION.SDK_INT < 34) {
//                                Method nativeSetupMethod2 = AudioRecord.class.getDeclaredMethod("native_setup", Object.class, Object.class, int[].class, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, int[].class, Parcel.class, Long.TYPE, Integer.TYPE);
//                                nativeSetupMethod2.setAccessible(true);
//                                initResult = ((Integer) nativeSetupMethod2.invoke(audioRecord, new WeakReference(audioRecord), attributes, sampleRateArray, Integer.valueOf(channelMask), 0, Integer.valueOf(audioRecord.getAudioFormat()), Integer.valueOf(bufferSizeInBytes), session, attributionSourceParcel, 0L, 0)).intValue();
//                            } else {
//                                Method nativeSetupMethod3 = AudioRecord.class.getDeclaredMethod("native_setup", Object.class, Object.class, int[].class, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, int[].class, Parcel.class, Long.TYPE, Integer.TYPE, Integer.TYPE);
//                                nativeSetupMethod3.setAccessible(true);
//                                initResult = ((Integer) nativeSetupMethod3.invoke(audioRecord, new WeakReference(audioRecord), attributes, sampleRateArray, Integer.valueOf(channelMask), 0, Integer.valueOf(audioRecord.getAudioFormat()), Integer.valueOf(bufferSizeInBytes), session, attributionSourceParcel, 0L, 0, 0)).intValue();
//                            }
//                            if (attributionSourceState != null) {
//                                attributionSourceState.close();
//                            }
//                        } catch (Throwable th2) {
//                            th = th2;
//                            if (attributionSourceState == null) {
//                                throw th;
//                            }
//                            try {
//                                attributionSourceState.close();
//                                throw th;
//                            } catch (Throwable th3) {
//                                th.addSuppressed(th3);
//                                throw th;
//                            }
//                        }
//                    } catch (Throwable th4) {
//                        th = th4;
//                    }
//                }
//                if (initResult != 0) {
//                    Ln.e("Error code " + initResult + " when initializing native AudioRecord object.");
//                    throw new RuntimeException("Cannot create AudioRecord");
//                }
//                Field mSampleRateField = AudioRecord.class.getDeclaredField("mSampleRate");
//                mSampleRateField.setAccessible(true);
//                mSampleRateField.set(audioRecord, Integer.valueOf(sampleRateArray[0]));
//                Field mSessionIdField = AudioRecord.class.getDeclaredField("mSessionId");
//                mSessionIdField.setAccessible(true);
//                mSessionIdField.set(audioRecord, Integer.valueOf(session[0]));
//                Field mStateField = AudioRecord.class.getDeclaredField("mState");
//                mStateField.setAccessible(true);
//                mStateField.set(audioRecord, 1);
//                return audioRecord;
//            } catch (Exception e) {
//                e = e;
//                Ln.e("Failed to invoke AudioRecord.<init>.", e);
//                throw new RuntimeException("Cannot create AudioRecord");
//            }
//        } catch (Exception e2) {
//            e = e2;
//        }
//    }
}